"""
HumanMakerInputFrame.py

Created by Ryan Walters on 2008-09-24.
Modeled after Steve Streza's NewGameFrame.
"""

import sys
import string
import os
from Tkinter import *

class HumanMakerInputFrame(Frame):
    """allows a human to input the maker move. this frame is inserted
       into the GameInputFrame and callsback after it collects the input
    """

    responseButs = [Button(),Button(),Button(),Button()]
    colorIndex = ["gray","white","black"]
    
    def __init__(self, master, doneHandler, **kv):
        """constructor. doneHandler is the callback method"""
        Frame.__init__(self, master, kv)

        # These variables are globals representing the values of the response
        self.responseVals = [0, 0, 0, 0]
        
        #this is the functor value to be invoked
        self.doneHandler = doneHandler
        
        # These items represent the buttons which will be found in the rows of
        # the input
        self.responseButs[0] = Button(self, height=2, width=4, bd = 5)
        self.responseButs[0].config(command=self.changeButtonZero, bg=self.colorIndex[0])
        self.responseButs[1] = Button(self, height=2, width=4, bd = 5)
        self.responseButs[1].config(command=self.changeButtonOne, bg=self.colorIndex[0])
        self.responseButs[2] = Button(self, height=2, width=4, bd = 5)
        self.responseButs[2].config(command=self.changeButtonTwo, bg=self.colorIndex[0])
        self.responseButs[3] = Button(self, height=2, width=4, bd = 5)
        self.responseButs[3].config(command=self.changeButtonThree, bg=self.colorIndex[0])

        #This button handles sending the input to the controller.  
        self.doneButton = Button(self, text="Done", command=self.handleDone)
        self.doneButton.config(height=2, width=12, bd = 3)

        #Place the buttons
        self.responseButs[0].grid(row=1, column=0)
        self.responseButs[1].grid(row=1, column=1)
        self.responseButs[2].grid(row=2, column=0)
        self.responseButs[3].grid(row=2, column=1)
        self.doneButton.grid(row=1, column=2, rowspan=2)

    def changeButtonZero(self):
        """changes the color of the first button"""
        self.changeButton(0)
        #self.response0.config (text=self.responsevals[0])

    def changeButtonOne(self):
        """changes the color of the second button"""
        self.changeButton(1)
        #self.response1.config (text=self.responsevals[1])

    def changeButtonTwo(self):
        """changes the color of the third button"""
        self.changeButton(2)
        #self.response2.config (text=self.responsevals[2])

    def changeButtonThree(self):
        """changes the color of the forth button"""
        self.changeButton(3)
        #self.response3.config (text=self.responsevals[3])

    def changeButton(self, buttonNum):
        self.responseVals[buttonNum] = self.responseVals[buttonNum] + 1
        self.responseVals[buttonNum] = self.responseVals[buttonNum] % 3
        self.responseButs[buttonNum].config (bg=self.colorIndex[self.responseVals[buttonNum]])
        if (self.responseVals[buttonNum] == 0):
            self.responseButs[buttonNum].config(bd = 5)
        else:
            self.responseButs[buttonNum].config(bd = 10)

    def handleDone(self):
        """callback"""
        self.doneHandler(self.responseVals)

    """def increment(self, index):
        self.responsevals[index] = self.responsevals[index] + 1
        if self.responsevals[index] > 2:
            self.responsevals[index] = 0
        #code would go here for changing the image; for now, instead,
        #have the text change
        if index is 0:
            self.response1.text = self.responsevals[index]
        elif index is 1:
            self.response2.text = self.responsevals[index]
        elif index is 2:
            self.response3.text = self.responsevals[index]
        else:
            self.response4.text = self.responsevals[index]"""

    def reset(self):
        """reset the buttons"""
        self.responseVals = [0, 0, 0, 0]
        self.responseButs[0].config (text=self.responseVals[0])
        self.responseButs[1].config (text=self.responseVals[1])
        self.responseButs[2].config (text=self.responseVals[2])
        self.responseButs[3].config (text=self.responseVals[3])

